package com.atguigu.tingshu.album.api;

import 	com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.annotation.TingshuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private SearchFeignClient searchFeignClient;


	@PostMapping(value = "/uploadTrack")
	@Operation(summary = "上传声音")
	public Result uploadTrack(MultipartFile file) {
		Map<String, Object> map = trackInfoService.uploadTrack(file);
		return Result.ok(map);
	}



	@PostMapping("/saveTrackInfo")
	@Operation(summary = "保存声音信息")
	@TingshuLogin
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
		Long albumId = trackInfoService.saveTrackInfo(trackInfoVo);
		searchFeignClient.onAlbumEs(albumId);
		return Result.ok();
	}

	@PostMapping("/findUserTrackPage/{pn}/{pz}")
	@Operation(summary = "查询登录用户的声音分页展示")
	@TingshuLogin
	public Result findUserTrackPage(@PathVariable(value = "pn") Long pn,
									@PathVariable(value = "pz") Long pz,
									@RequestBody TrackInfoQuery trackInfoQuery) {
		// 1.构建分页对象
		IPage<TrackListVo> trackInfoVoPage = new Page<>(pn, pz);
		trackInfoQuery.setUserId(AuthContextHolder.getUserId()); // 给条件对象赋值用户
		trackInfoVoPage = trackInfoService.findUserTrackPage(trackInfoVoPage, trackInfoQuery);
		return Result.ok(trackInfoVoPage); // 前端未来要的是分页对象里面的对象
	}


	@GetMapping(value = "/getTrackInfo/{trackId}")
	@Operation(summary = "回显声音")
	public Result getTrackInfo(@PathVariable(value = "trackId") Long trackId) {
		TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);
		return Result.ok(trackInfo);
	}

	@PutMapping("/updateTrackInfo/{trackId}")
	@Operation(summary = "修改声音")
	@TingshuLogin
	public Result updateTrackInfo(@RequestBody TrackInfoVo trackInfoVo,
								  @PathVariable(value = "trackId") Long trackId) {
		trackInfoService.updateTrackInfo(trackInfoVo, trackId);
		searchFeignClient.onAlbumEs(trackInfoVo.getAlbumId());
		return Result.ok();
	}

	@PutMapping("/removeTrackInfo/{trackId}")
	@Operation(summary = "删除声音")
	@TingshuLogin   // 校验用户是否登录
	public Result removeTrackInfo(@PathVariable(value = "trackId") Long trackId) {
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}



	// 问题一：要展示专辑下对应的声音是否要付费（通过图标展示）
	// 如果未来有付费图标，那么该声音就需要付费
	// 如果未来没有付费图标，那么该声音就免费
	// 问题二：查询该接口是否要求用户一定登录。
	// 1.可以不登录，直接进来看
	// 2.可以登录，在来看
	@GetMapping("/findAlbumTrackPage/{albumId}/{pn}/{pz}")
	@Operation(summary = "根据专辑id查询专辑下的声音列表")
	@TingshuLogin(required = false)
	public Result findAlbumTrackPage(@PathVariable(value = "albumId") Long albumId,
									 @PathVariable(value = "pn") Long pn,
									 @PathVariable(value = "pz") Long pz) {

		IPage<AlbumTrackListVo> albumTrackListVoPage = new Page<>(pn, pz);
		albumTrackListVoPage = trackInfoService.findAlbumTrackPage(albumId, albumTrackListVoPage);
		return Result.ok(albumTrackListVoPage);
	}

	@Operation(summary = "查询声音的各个维度值")
	@GetMapping("/getTrackStatVo/{trackId}")
	@Parameters({
			@Parameter(name = "trackId", description = "声音id", required = true)
	})
	public Result getTrackStatVo(@PathVariable(value = "trackId") Long trackId) {

		TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
		return Result.ok(trackStatVo);
	}
	@GetMapping("/findUserTrackPaidList/{currentTrackId}")
	@Operation(summary = "分集展示要购买的声音列表")
	@Parameters({
			@Parameter(name = "currentTrackId", description = "当前播放声音id", required = true)
	})
	@TingshuLogin
	public Result findUserTrackPaidList(@PathVariable(value = "currentTrackId") Long currentTrackId) {
		List<Map<String, Object>> trackPaidMapList = trackInfoService.findUserTrackPaidList(currentTrackId);
		return Result.ok(trackPaidMapList);
	}


}

